package com.example.pinkcat.fragment.shouye;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import com.example.pinkcat.R;
import com.example.pinkcat.adapter.GallerView_RecyclerViewAdapter;
import com.example.pinkcat.api.entity.VideowaibuEntity;
import com.example.pinkcat.api.getData.getData_waibu;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class Fragment_tuijian extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
    private Banner mBanner;

    Context context;

    private Fragment_tuijian_GallerViewModel fragment_tuijian_gallerViewModel;

    public RecyclerView recyclerView;
    private RecyclerView.LayoutManager mLayoutManager;

    private List<VideowaibuEntity> items = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;


    GallerView_RecyclerViewAdapter gallerView_recyclerViewAdapter;



    public Fragment_tuijian() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_tuijian,container,false);
    }



    @Override
    public void onResume() {
        super.onResume();


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = view.getContext();
        //瀑布流
        //找到列表
        recyclerView = (RecyclerView) view.findViewById(R.id.RecylceVW_manga);
        //设置布局类型
//        mLayoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager  bh= new GridLayoutManager(view.getContext(),2);
        bh.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(position==0){
                    return 2;
                }else
                    return 1;
            }
        });

        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.srl);//初始
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.pink,R.color.purple_200);

        recyclerView.setLayoutManager(bh);


        //设置数据
        gallerView_recyclerViewAdapter =new GallerView_RecyclerViewAdapter(view.getContext(),Fragment_tuijian.this);
        recyclerView.setAdapter(gallerView_recyclerViewAdapter);

        getData_waibu.getbannervideo(getContext(),gallerView_recyclerViewAdapter);
        getData_waibu.getrecommendVideoByuser(getContext(),gallerView_recyclerViewAdapter);

    }

//下拉刷新
public void onRefresh(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("fragment","下拉刷新");
                 int pageNumer = 0;;
                 PostData(); //加载数据
                swipeRefreshLayout.setRefreshing(false);
            }
        },1000);
}


public void PostData(){
    getData_waibu.getrecommendVideoByuser(context,gallerView_recyclerViewAdapter);
}



    
}
